package com.asia.algorithmcode.array;

import java.util.Arrays;

/**
 * @DESCRIPTION: 88、合并两个有序数组
 * @USER: wanfu
 * @DATE: 2025/4/17 星期四 17:51
 */
public class Merge {

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 0, 0, 0};
        int[] arr2 = {2, 5, 6};
        new Merge().merge(arr1, 3, arr2, 3);

        for (int i = 0; i < arr1.length; i++) {
            System.out.printf(arr1[i] + "\t");
        }
    }


    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1;
        int p2 = n - 1;
        int cur = 0;
        int length = m + n - 1;
        while (p1 > -1 || p2 > -1) {
            if (p1 == -1) {
                cur = nums2[p2--];
            } else if (p2 == -1) {
                cur = nums1[p1--];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums2[p2--];
            } else {
                cur = nums1[p1--];
            }
            nums1[length--] = cur;
        }
    }

}
